package com.xing.fragment;

import java.util.List;
import java.util.UUID;

import com.xing.adapter.SurveyListSecondAdapter;
import com.xing.controller.DBController;
import com.xing.model.HsdDataResearch;
import com.xing.questionnairesurvey.R;
import com.xing.util.FragmentNameFlagList;
import com.xing.util.ViewUtil;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class SurveyListSecondFragment extends Fragment{
	
	private ListView listView;
	private View view;
	private TextView titleTextView;
	private FragmentManager fragmentManager;
	private Button createQuestionnaireButton;
	private Button backButton;
	private String surveyuuid;
	public static String surveyName;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub 
		fragmentManager = getFragmentManager();
		surveyName = null;
		return (view = inflater.inflate(R.layout.survey_second_fragment,container,false));
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		surveyuuid = this.getArguments().getString("surveyuuid");
		surveyName = this.getArguments().getString("survey_name");
		backButton = (Button) view.findViewById(R.id.back);
		backButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				fragmentManager .popBackStack();
			}
		});
		titleTextView = (TextView) view.findViewById(R.id.title);
		titleTextView.setText(surveyName);
		titleTextView.setFocusable(true);
		titleTextView.setFocusableInTouchMode(true);
		listView = (ListView) view.findViewById(R.id.listview);
		createQuestionnaireButton = (Button) view.findViewById(R.id.create_questionnaire);
		createQuestionnaireButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
				fragmentTransaction.hide(fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY_SECOND_FRAGMENT));
				CreateHsdDataResearchFragment createHsdDataResearchFragment = new CreateHsdDataResearchFragment(); 
				Bundle bundle = new Bundle();
				/**
				 * 调查问卷的id
				 */
				bundle.putString("surveyuuid", surveyuuid);
				createHsdDataResearchFragment.setArguments(bundle);
				fragmentTransaction.add(R.id.fragmentRoot, createHsdDataResearchFragment, FragmentNameFlagList.CREATE_HSDDATARESEARCH_FRAGMENT);
				fragmentTransaction.addToBackStack(FragmentNameFlagList.CREATE_HSDDATARESEARCH_FRAGMENT);
				fragmentTransaction.commit();
			}
		});	
		
		DBController.getInstance().getHsdDataResearchListInDetail(getActivity(), surveyuuid, handler);
	}
	
	public void onHiddenChanged(boolean hidden) {
		if (!hidden) {
			DBController.getInstance().getHsdDataResearchListInDetail(getActivity(), surveyuuid, handler);
		}
	};
	
	private Handler handler =  new Handler(){
		public void handleMessage(android.os.Message msg) {
			List<HsdDataResearch> list = (List<HsdDataResearch>) msg.obj;
			if (list == null || list.size() == 0) {
				ViewUtil.showToast(getActivity(), "还没有已建立的问卷");
			}else {
				SurveyListSecondAdapter adapter = new SurveyListSecondAdapter(getActivity(), list,fragmentManager);
				listView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			}
			
		}
	};
	
	
}
